package com.example.thegreatwall.service.impl;

import com.example.thegreatwall.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.thegreatwall.mapper.UsersMapper;
import com.example.thegreatwall.entity.Users;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {
    private final ConcurrentHashMap<String, Users> fakeDB = new ConcurrentHashMap<>();
    private long idCounter = 1;
    @Autowired
    private UsersMapper userMapper;
    @Override
    public List<Users> findAll() {
        return userMapper.findAll();
    }

    /**
     * 登录方法
     *
     * @param userName 用户名
     * @param pwd      密码
     * @return 用户对象
     */

    /**
     * 微信小程序获取用户信息
     * @param openid
     * @return 用户对象
     */
    public Users findOrCreateUser(String openid) {
        Users user = userMapper.findByOpenid(openid);
        //如果为空，则是第一次微信登录，写入数据库做新增
        if (user == null) {
            user = new Users();
            user.setOpenid(openid);
            user.setUserName("微信用户");
            user.setUserProfile("https://dengqirong123.oss-cn-hangzhou.aliyuncs.com/assets/testUser_touxiang/3333.jpg"); //默认头像
            userMapper.insertUser(user);
        }
        return user;
    }

    /**
     * 根据用户id获得opengId
     *
     * @param userId 用户ID
     * @return
     */
    @Override
    public String getOpenIdByUserId(Long userId) {
        Users user = userMapper.selectById(userId);
        if (user == null || user.getOpenid() == null) {
            throw new RuntimeException("用户不存在或未绑定 OpenId");
        }
        return user.getOpenid();
    }

    @Override
    public int checkstatus(int user_id) {
        return this.checkstatus(user_id);
    }


}
